﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using dbutility;
using DBUtility;
using System.Collections.Generic;
namespace hf.DAL
{
	/// <summary>
	/// 数据访问类:Wanted
	/// </summary>
	public partial class Wanted
	{
		public Wanted()
		{}
		#region  Method

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Wanted> GetWantedList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            DataSet ds = PageHelper.GetList(PageSize, PageNumber, "tb_Wanted", "ID", strWhere, out total);

            //实例化列表
            List<hf.Model.Wanted> list = new List<hf.Model.Wanted>();

            //判断ds是不是空的
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //把datatable转换成对象
                    hf.Model.Wanted m = DBHelperSQL.SetModel<hf.Model.Wanted>(dr);

                    if (m != null)
                        list.Add(m);
                }
            }

            

            return list;
        }

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DBHelperSQL.GetMaxID("ID", "tb_Wanted"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int result= DBHelperSQL.RunProcedure("tb_Wanted_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public int Add(hf.Model.Wanted model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@JobsID", SqlDbType.Int,4),
					new SqlParameter("@Names", SqlDbType.VarChar,50),
					new SqlParameter("@Sex", SqlDbType.Int,4),
					new SqlParameter("@Marriage", SqlDbType.Int,4),
					new SqlParameter("@Birthday", SqlDbType.DateTime),
					new SqlParameter("@Height", SqlDbType.Int,4),
					new SqlParameter("@PicUrl", SqlDbType.VarChar,255),
					new SqlParameter("@School", SqlDbType.VarChar,255),
					new SqlParameter("@Specialty", SqlDbType.VarChar,100),
					new SqlParameter("@Academic", SqlDbType.VarChar,100),
					new SqlParameter("@Language", SqlDbType.VarChar,100),
					new SqlParameter("@Computer", SqlDbType.VarChar,100),
					new SqlParameter("@isExamination", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@PersonWeb", SqlDbType.VarChar,255),
					new SqlParameter("@AccountPlace", SqlDbType.VarChar,100),
					new SqlParameter("@NowPlace", SqlDbType.VarChar,100),
					new SqlParameter("@Addr", SqlDbType.VarChar,100),
					new SqlParameter("@WorkProvinceID", SqlDbType.Int,4),
					new SqlParameter("@WorkCityID", SqlDbType.Int,4),
					new SqlParameter("@Treatment", SqlDbType.VarChar,255),
					new SqlParameter("@PersonInfo", SqlDbType.VarChar,255),
					new SqlParameter("@Workexe", SqlDbType.VarChar,255),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Addtime", SqlDbType.DateTime)};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = model.JobsID;
			parameters[2].Value = model.Names;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.Marriage;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.Height;
			parameters[7].Value = model.PicUrl;
			parameters[8].Value = model.School;
			parameters[9].Value = model.Specialty;
			parameters[10].Value = model.Academic;
			parameters[11].Value = model.Language;
			parameters[12].Value = model.Computer;
			parameters[13].Value = model.isExamination;
			parameters[14].Value = model.Phone;
			parameters[15].Value = model.Email;
			parameters[16].Value = model.PersonWeb;
			parameters[17].Value = model.AccountPlace;
			parameters[18].Value = model.NowPlace;
			parameters[19].Value = model.Addr;
			parameters[20].Value = model.WorkProvinceID;
			parameters[21].Value = model.WorkCityID;
			parameters[22].Value = model.Treatment;
			parameters[23].Value = model.PersonInfo;
			parameters[24].Value = model.Workexe;
			parameters[25].Value = model.UserID;
			parameters[26].Value = model.Addtime;

			DBHelperSQL.RunProcedure("tb_Wanted_ADD",parameters,out rowsAffected);
			return (int)parameters[0].Value;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public bool Update(hf.Model.Wanted model)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@JobsID", SqlDbType.Int,4),
					new SqlParameter("@Names", SqlDbType.VarChar,50),
					new SqlParameter("@Sex", SqlDbType.Int,4),
					new SqlParameter("@Marriage", SqlDbType.Int,4),
					new SqlParameter("@Birthday", SqlDbType.DateTime),
					new SqlParameter("@Height", SqlDbType.Int,4),
					new SqlParameter("@PicUrl", SqlDbType.VarChar,255),
					new SqlParameter("@School", SqlDbType.VarChar,255),
					new SqlParameter("@Specialty", SqlDbType.VarChar,100),
					new SqlParameter("@Academic", SqlDbType.VarChar,100),
					new SqlParameter("@Language", SqlDbType.VarChar,100),
					new SqlParameter("@Computer", SqlDbType.VarChar,100),
					new SqlParameter("@isExamination", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@PersonWeb", SqlDbType.VarChar,255),
					new SqlParameter("@AccountPlace", SqlDbType.VarChar,100),
					new SqlParameter("@NowPlace", SqlDbType.VarChar,100),
					new SqlParameter("@Addr", SqlDbType.VarChar,100),
					new SqlParameter("@WorkProvinceID", SqlDbType.Int,4),
					new SqlParameter("@WorkCityID", SqlDbType.Int,4),
					new SqlParameter("@Treatment", SqlDbType.VarChar,255),
					new SqlParameter("@PersonInfo", SqlDbType.VarChar,255),
					new SqlParameter("@Workexe", SqlDbType.VarChar,255),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@Addtime", SqlDbType.DateTime)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.JobsID;
			parameters[2].Value = model.Names;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.Marriage;
			parameters[5].Value = model.Birthday;
			parameters[6].Value = model.Height;
			parameters[7].Value = model.PicUrl;
			parameters[8].Value = model.School;
			parameters[9].Value = model.Specialty;
			parameters[10].Value = model.Academic;
			parameters[11].Value = model.Language;
			parameters[12].Value = model.Computer;
			parameters[13].Value = model.isExamination;
			parameters[14].Value = model.Phone;
			parameters[15].Value = model.Email;
			parameters[16].Value = model.PersonWeb;
			parameters[17].Value = model.AccountPlace;
			parameters[18].Value = model.NowPlace;
			parameters[19].Value = model.Addr;
			parameters[20].Value = model.WorkProvinceID;
			parameters[21].Value = model.WorkCityID;
			parameters[22].Value = model.Treatment;
			parameters[23].Value = model.PersonInfo;
			parameters[24].Value = model.Workexe;
			parameters[25].Value = model.UserID;
			parameters[26].Value = model.Addtime;

			DBHelperSQL.RunProcedure("tb_Wanted_Update",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			DBHelperSQL.RunProcedure("tb_Wanted_Delete",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_Wanted ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DBHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hf.Model.Wanted GetModel(int ID)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			hf.Model.Wanted model=new hf.Model.Wanted();
			DataSet ds= DBHelperSQL.RunProcedure("tb_Wanted_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["JobsID"]!=null && ds.Tables[0].Rows[0]["JobsID"].ToString()!="")
				{
					model.JobsID=int.Parse(ds.Tables[0].Rows[0]["JobsID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Names"]!=null && ds.Tables[0].Rows[0]["Names"].ToString()!="")
				{
					model.Names=ds.Tables[0].Rows[0]["Names"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Sex"]!=null && ds.Tables[0].Rows[0]["Sex"].ToString()!="")
				{
					model.Sex=int.Parse(ds.Tables[0].Rows[0]["Sex"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Marriage"]!=null && ds.Tables[0].Rows[0]["Marriage"].ToString()!="")
				{
					model.Marriage=int.Parse(ds.Tables[0].Rows[0]["Marriage"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Birthday"]!=null && ds.Tables[0].Rows[0]["Birthday"].ToString()!="")
				{
					model.Birthday=DateTime.Parse(ds.Tables[0].Rows[0]["Birthday"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Height"]!=null && ds.Tables[0].Rows[0]["Height"].ToString()!="")
				{
					model.Height=int.Parse(ds.Tables[0].Rows[0]["Height"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PicUrl"]!=null && ds.Tables[0].Rows[0]["PicUrl"].ToString()!="")
				{
					model.PicUrl=ds.Tables[0].Rows[0]["PicUrl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["School"]!=null && ds.Tables[0].Rows[0]["School"].ToString()!="")
				{
					model.School=ds.Tables[0].Rows[0]["School"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Specialty"]!=null && ds.Tables[0].Rows[0]["Specialty"].ToString()!="")
				{
					model.Specialty=ds.Tables[0].Rows[0]["Specialty"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Academic"]!=null && ds.Tables[0].Rows[0]["Academic"].ToString()!="")
				{
					model.Academic=ds.Tables[0].Rows[0]["Academic"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Language"]!=null && ds.Tables[0].Rows[0]["Language"].ToString()!="")
				{
					model.Language=ds.Tables[0].Rows[0]["Language"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Computer"]!=null && ds.Tables[0].Rows[0]["Computer"].ToString()!="")
				{
					model.Computer=ds.Tables[0].Rows[0]["Computer"].ToString();
				}
				if(ds.Tables[0].Rows[0]["isExamination"]!=null && ds.Tables[0].Rows[0]["isExamination"].ToString()!="")
				{
					model.isExamination=int.Parse(ds.Tables[0].Rows[0]["isExamination"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Phone"]!=null && ds.Tables[0].Rows[0]["Phone"].ToString()!="")
				{
					model.Phone=ds.Tables[0].Rows[0]["Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PersonWeb"]!=null && ds.Tables[0].Rows[0]["PersonWeb"].ToString()!="")
				{
					model.PersonWeb=ds.Tables[0].Rows[0]["PersonWeb"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AccountPlace"]!=null && ds.Tables[0].Rows[0]["AccountPlace"].ToString()!="")
				{
					model.AccountPlace=ds.Tables[0].Rows[0]["AccountPlace"].ToString();
				}
				if(ds.Tables[0].Rows[0]["NowPlace"]!=null && ds.Tables[0].Rows[0]["NowPlace"].ToString()!="")
				{
					model.NowPlace=ds.Tables[0].Rows[0]["NowPlace"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Addr"]!=null && ds.Tables[0].Rows[0]["Addr"].ToString()!="")
				{
					model.Addr=ds.Tables[0].Rows[0]["Addr"].ToString();
				}
				if(ds.Tables[0].Rows[0]["WorkProvinceID"]!=null && ds.Tables[0].Rows[0]["WorkProvinceID"].ToString()!="")
				{
					model.WorkProvinceID=int.Parse(ds.Tables[0].Rows[0]["WorkProvinceID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["WorkCityID"]!=null && ds.Tables[0].Rows[0]["WorkCityID"].ToString()!="")
				{
					model.WorkCityID=int.Parse(ds.Tables[0].Rows[0]["WorkCityID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Treatment"]!=null && ds.Tables[0].Rows[0]["Treatment"].ToString()!="")
				{
					model.Treatment=ds.Tables[0].Rows[0]["Treatment"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PersonInfo"]!=null && ds.Tables[0].Rows[0]["PersonInfo"].ToString()!="")
				{
					model.PersonInfo=ds.Tables[0].Rows[0]["PersonInfo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Workexe"]!=null && ds.Tables[0].Rows[0]["Workexe"].ToString()!="")
				{
					model.Workexe=ds.Tables[0].Rows[0]["Workexe"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					model.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Addtime"]!=null && ds.Tables[0].Rows[0]["Addtime"].ToString()!="")
				{
					model.Addtime=DateTime.Parse(ds.Tables[0].Rows[0]["Addtime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,JobsID,Names,Sex,Marriage,Birthday,Height,PicUrl,School,Specialty,Academic,Language,Computer,isExamination,Phone,Email,PersonWeb,AccountPlace,NowPlace,Addr,WorkProvinceID,WorkCityID,Treatment,PersonInfo,Workexe,UserID,Addtime ");
			strSql.Append(" FROM tb_Wanted ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,JobsID,Names,Sex,Marriage,Birthday,Height,PicUrl,School,Specialty,Academic,Language,Computer,isExamination,Phone,Email,PersonWeb,AccountPlace,NowPlace,Addr,WorkProvinceID,WorkCityID,Treatment,PersonInfo,Workexe,UserID,Addtime ");
			strSql.Append(" FROM tb_Wanted ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_Wanted ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from tb_Wanted T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_Wanted";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DBHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

